class CategoriesController < ApplicationController
  def index
    # product or subcategory /xxx/yyy/...
    unless params[:categories].blank?

      # try to find product
      @product = Product.where(:url => params[:categories].split('/').last.to_s).on_sale.first

      # product detail
      unless @product.blank?
        render_product

      # subcategory
      else
        # try to find subcategory
        @category = Category.where(:url => params[:categories].split('/').last.to_s).active.first

        unless @category.blank?
          render_subcategory
        else
          render_not_found
        end
      end

    # main category
    else
      @category = CategoryGroup.find(1).roots.where(:url => params[:main_category].to_s).active.first

      unless @category.blank?
        render_main_category
      else
        render_not_found
      end
    end
  end

  private
  def render_product

  end

  def render_subcategory
    render :action => "category"
  end

  def render_main_category
    render :action => "main_category"
  end
end
